;; console application

(in-package :gtcup)

(clsql:disable-sql-reader-syntax)
(clsql:enable-sql-reader-syntax)

(defparameter *server-gtcup* nil)

(defun run ()
  (let ((connection nil))
    (setf connection (create-database-connection
			"localhost"
			"gtcup"
			"root"
			nil))

    ;; Connect to database
    (connect-to-db connection)
    
    ;; Record the sql going out, helps us learn what is going
    ;; on behind the scenes
    (clsql:start-sql-recording)))

(defun start-all (port)
  (when (not port) (setf port 4344))
  (run)
  (setf *server-gtcup* (start-server :port port)))

(defun stop-all ()
  (stop-server *server-gtcup*)
  (quit))
  

(defun clean-create-test-data ()
  ;; Create the tables for our view classes
  ;; First we drop them, ignoring any errors
  (ignore-errors
    (clsql:drop-view-from-class 'circuito)
    (clsql:drop-view-from-class 'campeonato)
    (clsql:drop-view-from-class 'pontuacao)
    (clsql:drop-view-from-class 'corrida)      
    (clsql:drop-view-from-class 'piloto)
    (clsql:drop-view-from-class 'resultado))
  
  (clsql:create-view-from-class 'circuito)        
  (clsql:create-view-from-class 'campeonato)    
  (clsql:create-view-from-class 'pontuacao)    
  (clsql:create-view-from-class 'corrida)    
  (clsql:create-view-from-class 'piloto)
  (clsql:create-view-from-class 'resultado)
  
  (save (make-instance 'circuito :nome "Spa Francorchamps 2004" :arquivo "4Spa.TRK"))
  (save (make-instance 'circuito :nome "A1 Ring" :arquivo "a1ring.TRK"))
  (save (make-instance 'circuito :nome "Barcelona 2003" :arquivo "3Barcelona.TRK"))
  (save (make-instance 'circuito :nome "Anderstorp 2003" :arquivo "3Anderstorp.TRK"))
  (save (make-instance 'circuito :nome "Magny-Cours 2004" :arquivo "4Magny.TRK"))
  (save (make-instance 'circuito :nome "Monza 2004" :arquivo "4Monza.TRK"))
  (save (make-instance 'circuito :nome "Laguna Seca" :arquivo "LagunaSeca.TRK"))
  (save (make-instance 'circuito :nome "Brno 2004" :arquivo "4Brno.TRK"))
  (save (make-instance 'circuito :nome "Detroit Street" :arquivo "STDetroit.TRK"))
        
  (save (make-instance 'campeonato :nome "Corridas Fora de Campeonato"))
  (save (make-instance 'campeonato :nome "Endurance 2007"))
  
  
  ;; 20,18,16,14,13,12,11,10,9,8,7,6,5,4,3,2,1 
  (let ((campeonato (car 
                     (car 
                      (save 
                       (make-instance 'campeonato :nome "Porsche Bi-Turbo 2007-02"))))))
    (save (make-instance 'pontuacao :pontos 20 :posicao 1 :campeonato-id (id campeonato)))
    (save (make-instance 'pontuacao :pontos 18 :posicao 2 :campeonato-id (id campeonato)))
    (save (make-instance 'pontuacao :pontos 16 :posicao 3 :campeonato-id (id campeonato)))
    (save (make-instance 'pontuacao :pontos 14 :posicao 4 :campeonato-id (id campeonato)))
    (save (make-instance 'pontuacao :pontos 13 :posicao 5 :campeonato-id (id campeonato)))
    (save (make-instance 'pontuacao :pontos 12 :posicao 6 :campeonato-id (id campeonato)))
    (save (make-instance 'pontuacao :pontos 11 :posicao 7 :campeonato-id (id campeonato)))
    (save (make-instance 'pontuacao :pontos 10 :posicao 8 :campeonato-id (id campeonato)))
    (save (make-instance 'pontuacao :pontos 9 :posicao 9 :campeonato-id (id campeonato)))
    (save (make-instance 'pontuacao :pontos 8 :posicao 10 :campeonato-id (id campeonato)))
    (save (make-instance 'pontuacao :pontos 7 :posicao 11 :campeonato-id (id campeonato)))
    (save (make-instance 'pontuacao :pontos 6 :posicao 12 :campeonato-id (id campeonato)))
    (save (make-instance 'pontuacao :pontos 5 :posicao 13 :campeonato-id (id campeonato)))
    (save (make-instance 'pontuacao :pontos 4 :posicao 14 :campeonato-id (id campeonato)))
    (save (make-instance 'pontuacao :pontos 3 :posicao 15 :campeonato-id (id campeonato)))
    (save (make-instance 'pontuacao :pontos 2 :posicao 16 :campeonato-id (id campeonato)))
    (save (make-instance 'pontuacao :pontos 1 :posicao 17 :campeonato-id (id campeonato)))
    (import-result-file "/home/lukas/Projects/Workspace/gtcup/data/GTCup Brasil_2007_08_19_20_07_00_Race.txt" (id campeonato))
    (import-result-file "/home/lukas/Projects/Workspace/gtcup/data/GTCup Brasil_2007_08_26_20_04_35_Race.txt" (id campeonato))
    (import-result-file "/home/lukas/Projects/Workspace/gtcup/data/GTCup Brasil_2007_09_16_20_02_28_Race.txt" (id campeonato))
    (import-result-file "/home/lukas/Projects/Workspace/gtcup/data/GTCup Brasil_2007_09_30_20_02_48_Race.txt" (id campeonato))
    (import-result-file "/home/lukas/Projects/Workspace/gtcup/data/GTCup Brasil_2007_10_14_19_00_24_Race.txt" (id campeonato))
    (import-result-file "/home/lukas/Projects/Workspace/gtcup/data/GTCup Brasil_2007_10_28_19_29_10_Race.txt" (id campeonato))
    (import-result-file "/home/lukas/Projects/Workspace/gtcup/data/GTCup Brasil_2007_11_25_18_15_09_Race.txt" (id campeonato))
    (import-result-file "/home/lukas/Projects/Workspace/gtcup/data/GTCup Brasil_2007_12_09_18_14_50_Race.txt" (id campeonato)))
      
  (let ((campeonato (car (car (select 'campeonato :where
                                      [= [slot-value 'campeonato 'nome] 
                                      "Corridas Fora de Campeonato"])))))
    (import-result-file "/home/lukas/Projects/Workspace/gtcup/data/data.txt" (id campeonato))
    (import-result-file "/home/lukas/Projects/Workspace/gtcup/data/data2.txt" (id campeonato))
    (import-result-file "/home/lukas/Projects/Workspace/gtcup/data/data3.txt" (id campeonato)))
  (let ((corridas (select 'corrida)))
    (dolist (uma-corrida corridas)
      (save-pontuacao-by-corrida (car uma-corrida)))))

(defun main-loop ()
  (let ((option nil))
    (loop
     (setf option (prompt-read "Select Option: (1:Add, 2:List 3:Quit)"))
     (if (equalp "1" option)
         (prompt-for-site))
     (if (equalp "2" option)
         (= 1 1))
     (if (equalp "3" option)
         (return)))))

(defun prompt-for-site ()
  (clsql:update-records-from-instance (make-instance 'corrida
                                                     :nome (prompt-read "Nome"))))


(defun prompt-read (prompt)
  (format *query-io* "~a: " prompt)
  (force-output *query-io*)
  (read-line *query-io*))

(defun quit ()
  (clsql:disconnect))
